package com.jieshao.setting

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.util.xmlb.XmlSerializerUtil
import com.jieshao.rule.*
import com.jieshao.utils.PLUGIN_ID

var rules: List<Rule> = listOf(
    GitHub(),
    GitLab(),
    BitbucketCloud(),
    BitbucketServer(),
    Gitee(),
    Gitea(),
    Gogs(),
    Srht(),
    Azure(),
    Chromium(),
    Gerrit()
)

/**
 * 主要保管全局的模板配置
 */
@State(name = PLUGIN_ID, storages = [Storage("LineShare.xml")])
class ApplicationSettings : PersistentStateComponent<ApplicationSettings?> {

    val customRules: MutableList<Rule> = mutableListOf()

    override fun getState() = this

    override fun loadState(state: ApplicationSettings) {
        XmlSerializerUtil.copyBean(state, this)
    }

    fun findRuleByCustomDomain(origin: String): Rule? {
        return (customRules + rules).find {
            origin.matches(it.originMatch)
        }
    }

    fun getAllRule(): List<Rule> {
        return (customRules + rules)
    }
}